package ecoiot.com.testproject.webview;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by developer on 17-11-14.
 */

public class IoUtils {

    public static void close(Closeable paramCloseable) throws IOException {
        if (paramCloseable != null) {}
        paramCloseable.close();
        return;
    }

    public static long copy(File paramFile, OutputStream paramOutputStream)
            throws IOException
    {
        return copy(new FileInputStream(paramFile), paramOutputStream);
    }

    public static long copy(InputStream paramInputStream, File paramFile)
            throws IOException
    {
        return copy(paramInputStream, new FileOutputStream(paramFile));
    }

    public static long copy(InputStream paramInputStream, OutputStream paramOutputStream)
            throws IOException
    {
        try
        {
            long l = copy0(paramInputStream, paramOutputStream);
            return l;
        }
        finally
        {
            close(paramInputStream);
            close(paramOutputStream);
        }
    }

    public static long copy0(InputStream paramInputStream, OutputStream paramOutputStream)
            throws IOException
    {
        byte[] arrayOfByte = new byte['1'];
        int i;
        long l = 0L;
        for (;; l += i)
        {
            i = paramInputStream.read(arrayOfByte);
            if (-1 == i) {
                break;
            }
            paramOutputStream.write(arrayOfByte, 0, i);
        }
        paramOutputStream.flush();
        return l;
    }

    public static String readString(File paramFile)
            throws IOException
    {
        return readString(new FileInputStream(paramFile));
    }

    public static String readString(InputStream paramInputStream)
            throws IOException
    {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        copy(paramInputStream, localByteArrayOutputStream);
        return new String(localByteArrayOutputStream.toByteArray(), "UTF-8");
    }

    public static void writeString(String paramString, File paramFile)
            throws IOException
    {
        copy(new ByteArrayInputStream(paramString.getBytes()), new FileOutputStream(paramFile));
    }

    public static void writeString(String paramString, OutputStream paramOutputStream)
            throws IOException
    {
        copy(new ByteArrayInputStream(paramString.getBytes()), paramOutputStream);
    }
}
